package moe.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DefaultParamBinder<T> implements ParamBinder {
	private T[] params;

	public DefaultParamBinder(T... params) {
		this.params = params;
	}

	public void bind(PreparedStatement stat) throws SQLException {
		for (int i = 0, length = params.length; i < length; i++) {
			T param = params[i];
			if (param instanceof String) {
				stat.setString(i + 1, param.toString());
			} else if (param instanceof Number) {
				Number num = (Number) param;
				if (num instanceof Float || num instanceof Double) {
					stat.setDouble(i + 1, num.doubleValue());
				} else {
					stat.setLong(i + 1, num.longValue());
				}
			} else if (param instanceof Boolean) {
				stat.setBoolean(i + 1, (Boolean) param);
			} else {
				stat.setObject(i + 1, param);
			}
		}
	}
}
